/**
 *
 */
package org.xshop.core;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xshop.service.ISysParamService;

import cn.jpush.api.JPushClient;
import top.ibase4j.core.support.push.BaseJPush;

/**
 *
 * @author ShenHuaJie
 * @version 2017年11月8日 下午4:54:54
 */
@Component
public class JPush2Helper extends BaseJPush {
    private Logger logger = LogManager.getLogger();
    @Autowired
    private ISysParamService sysParamService;
    private JPushClient jpushClient;

    private String getParam(String key) {
        String result = sysParamService.getValue(key);
        return result;
    }

    @Override
    public JPushClient getJPushClient() {
        if (jpushClient == null) {
            try {
                String appKey = getParam("EXPRESS-APP-KEY");
                String masterSecret = getParam("EXPRESS-APP-SECRET");
                jpushClient = new JPushClient(masterSecret, appKey);
            } catch (Exception e) {
                logger.error("初始化极光推送客户端失败：", e);
            }
        }
        if (jpushClient != null) {
            return jpushClient;
        } else {
            try {
                Thread.sleep(1000 * 5);
            } catch (InterruptedException e) {
                logger.error("", e);
            }
            return getJPushClient();
        }
    }
}
